package gr.gsis.efmy.view;

import java.util.logging.Logger;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class DataTable extends Composite {
	
	private static Logger LOGGER = Logger.getLogger("DataTable");
	
	private Shell shell;
	
	private Table table;
	
	public DataTable(Shell shell) {
		super(shell, SWT.CASCADE);
		this.shell = shell;
		init();
	}
	
	private void init() {
		this.setLayout(new FillLayout());
		table = new Table(this, SWT.BORDER | SWT.MULTI);
		
		createTableHeaders();
		createTableMenu();
		
		table.setLinesVisible (true);
		
		final TableEditor editor = new TableEditor (table);
		editor.horizontalAlignment = SWT.LEFT;
		editor.grabHorizontal = true;
		table.addListener (SWT.MouseDown, new Listener () {
			public void handleEvent (Event event) {
				Rectangle clientArea = table.getClientArea ();
				Point pt = new Point (event.x, event.y);
				int index = table.getTopIndex ();
				while (index < table.getItemCount ()) {
					boolean visible = false;
					final TableItem item = table.getItem (index);
					for (int i=0; i<table.getColumnCount (); i++) {
						Rectangle rect = item.getBounds (i);
						if (rect.contains (pt)) {
							final int column = i;
							final Text text = new Text (table, SWT.NONE);
							Listener textListener = new Listener () {
								public void handleEvent (final Event e) {
									switch (e.type) {
										case SWT.FocusOut:
											item.setText (column, text.getText ());
											text.dispose ();
											break;
										case SWT.Traverse:
											switch (e.detail) {
												case SWT.TRAVERSE_RETURN:
													item.setText (column, text.getText ());
													//FALL THROUGH
												case SWT.TRAVERSE_ESCAPE:
													text.dispose ();
													e.doit = false;
											}
											break;
									}
								}
							};
							text.addListener (SWT.FocusOut, textListener);
							text.addListener (SWT.Traverse, textListener);
							editor.setEditor (text, item, i);
							text.setText (item.getText (i));
							text.selectAll ();
							text.setFocus ();
							return;
						}
						if (!visible && rect.intersects (clientArea)) {
							visible = true;
						}
					}
					if (!visible) return;
					index++;
				}
			}
		});
	}

	private void createTableHeaders() {
		table.setHeaderVisible(true);
		
		String[] titles={"ΑΦΜ", "ΕΠΩΝΥΜΟ", "ΟΝΟΜΑ", "ΠΑΤΡΩΝΥΜΟ", 
							"ΠΟΛΗ", "ΟΔΟΣ", "ΑΡΙΘΜΟΣ", "Τ.Κ.", "ΕΙΔΟΣ ΑΠΟΔΟΧΩΝ",
							"ΑΚΑΘΑΡΙΣΤΕΣ ΑΠΟΔΟΧΕΣ", "ΧΑΡΤΟΣΗΜΟ", "ΟΓΑ ΧΑΡΤΟΣΗΜΟ", 
							"ΚΡΑΤΗΣΕΙΣ (ΕΚΤΩΣ ΦΟΡΟΥ) (*)", "ΚΑΘΑΡΕΣ ΑΠΟΔΟΧΕΣ (*)"}; 

		for (String title : titles) {
			TableColumn column = new TableColumn (table, SWT.NONE);
			column.setText(title);
			column.setWidth(100);
		}
		
		final TableColumn column1 = table.getColumn(0);
		
		Listener sortListener = new Listener() {
			@Override
			public void handleEvent(Event e) {
				// determine new sort column and direction
				TableColumn sortColumn = table.getSortColumn();
				TableColumn currentColumn = (TableColumn) e.widget;
				int dir = table.getSortDirection();
				if (sortColumn == currentColumn) {
					dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
				} else {
					table.setSortColumn(currentColumn);
					dir = SWT.UP;
				}
				// sort the data based on column and direction
				final int index = currentColumn == column1 ? 0 : 1;
				final int direction = dir;
				
				/*
				Arrays.sort(data, new Comparator() {
					public int compare(Object arg0, Object arg1) {
						int[] a = (int[]) arg0;
						int[] b = (int[]) arg1;
						if (a[index] == b[index]) return 0;
						if (direction == SWT.UP) {
							return a[index] < b[index] ? -1 : 1;
						}
						return a[index] < b[index] ? 1 : -1;
					}
				});
				*/
				// update data displayed in table
				table.setSortDirection(dir);
				table.clearAll();
			}
		};
		
		
		column1.addListener(SWT.Selection, sortListener);
		table.setSortColumn(column1);
		//table.setSortDirection(SWT.UP);
	}
	
	private void createTableMenu() {
		Menu menu = new Menu(shell, SWT.POP_UP);
		table.setMenu(menu);
		
		MenuItem item = new MenuItem(menu, SWT.PUSH);
		item.setText("Add Record");
		item.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event e) {
				TableItem item = new TableItem(table, SWT.NONE);
				item.setText("New line");
			}
		});
	}
	
	public TableItem[] getItems() {		
		return table.getItems();
	}
	
	public int getColumnCount() {
		return table.getColumnCount();
	}
}

